package com.example.administrator.demo_one.VO;

/**
 * Created by ykbboom on 2017/1/21 17:26
 * email：ykbpro@whut.edu.cn
 */

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.demo_one.R;

import java.util.List;

/**
 * @author ykbboom
 * 功能:RecyclerView的适配器
*/
public class PeopleAdapter_2 extends RecyclerView.Adapter<PeopleAdapter_2.ViewHolder> {
    private List<People>peoples;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View peopleView;
        ImageView peopleImage;
        TextView peopleName;
        public ViewHolder(View view){
            super(view);
            peopleView = view;
            peopleImage = (ImageView) view.findViewById(R.id.peopleimage);
            peopleName = (TextView) view.findViewById(R.id.name);
        }
    }

    public PeopleAdapter_2(List<People>peoples){
        this.peoples = peoples;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        //注册点击事件
        viewHolder.peopleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                People people = peoples.get(viewHolder.getAdapterPosition());
                Toast.makeText(v.getContext(),"你点击了"+people.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        viewHolder.peopleImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                People people = peoples.get(viewHolder.getAdapterPosition());
                Toast.makeText(v.getContext(),"我为长者续一秒！！",Toast.LENGTH_SHORT).show();
            }
        });
        return  viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        People people = peoples.get(position);
        holder.peopleName.setText(people.getName());
        holder.peopleImage.setImageResource(people.getImageId());
    }

    @Override
    public int getItemCount() {
        return peoples.size();
    }
}
